#include "client.h"
#include "structures.h"
#include "neighbour.h"
#include "serveur.h"

/**
 * create argv[1] clients child for tests
 */
void multiple_client(int argc, char **argv){
	int i, pid, nchildren = 1;
	if (argc>1)
		nchildren = atoi(argv[1]);

	for (i=0; i<nchildren; i++){
		if ((pid = fork()) == 0){
			create_client(i+1);
			exit(0);
		}
	}
	wait(NULL);
}

/**
 * Creation du client de numero childnum
 */

void create_client(int num){
	int ret, nbyte, i, sock;
	struct sockaddr_in sAddr;
	char buf[BUFSIZE];

	//Configuration adresse
	memset((void *)&sAddr, 0, sizeof(sAddr));
	sAddr.sin_family = AF_INET;
	sAddr.sin_addr.s_addr = inet_addr(IP);
	sAddr.sin_port = 0;//htons(PORT_CLT);
	


	sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	CHK_ERR(sock, "soket");

	ret = bind(sock, (const struct sockaddr *) &sAddr, sizeof(sAddr));
	CHK_ERR(ret, "bind");

	sAddr.sin_port = htons(PORT);
	
	ret = connect(sock, (const struct sockaddr *) &sAddr, sizeof(sAddr));
	CHK_ERR(ret, "connect");
	int a = 0;
	/*
	identifiants ident;

	ident.port = sAddr.sin_port;
	ident.ip.s_addr = sAddr.sin_addr.s_addr;
	printf("ip : %u\t port: %d\n", ident.ip.s_addr, ident.port);
	//table[0] = &ident;
	propagation(&ident);*/

	//attention : multiplexage
	fd_set readset;
	int fdmax = -1;
	int rd[2]; // les descripteur a surveiller
	rd[0] = STDIN_FILENO;
	rd[1] = sock;
	FD_ZERO(&readset);

	while(1){

		//attente en lecture sur rd[i]
		FD_ZERO(&readset);
		for(i=0; i<2; i++){
			FD_SET(rd[i], &readset);
			if (rd[i]>fdmax) fdmax = rd[i];
		}
		//printf("e\n");
		ret = select(fdmax+1, &readset,NULL,NULL,NULL);
		CHK_ERR(ret, "select");
		//printf("s\n");
		if (ret > 0){
			for(i=0; i<2; i++){
				if (FD_ISSET(rd[i], &readset)){
					//lire sur r[i], un recv est un read:)
					nbyte = read(rd[i], buf, BUFSIZE);
					CHK_ERR(nbyte, "read");
					if ( i == 0) {
						buf[nbyte] = '\0';
						nbyte++;
						ret = send(sock, buf, nbyte, 0);
						printf("client #%i send %i chars : %s\n", num, ret, buf);
						//handler_cmd_response(buf,sock);
						//printf("end send\n");
					} else if( nbyte != -1 && nbyte != 0)
						printf("client #%i received %i chars : %s\n", num, nbyte, buf);
				}
			}
		}
		// partie (marche) sans multiplexage
		//						printf("read\n");
		//						nbyte = read(STDIN_FILENO, buf, BUFSIZE);
		//						CHK_ERR(nbyte, "read");
		//						buf[nbyte] = '\0'; //pour afficher chaine de caractere
		//						nbyte++;
		//						ret = send(sock, buf, nbyte, 0); //bloquant
		//						printf("client #%i send %i chars : %s\n", num, ret, buf);
		//
		//						printf("%c\n",buf[nbyte]);
		//
		//						sleep(1);
		//
		//						printf("recv\n");
		//						nbyte = recv(sock, buf, BUFSIZE, MSG_DONTWAIT); //non bloquant MSG_DONTWAIT
		//						if( nbyte != -1 && nbyte != 0)
		//							printf("client #%i received %i chars : %s\n", num, nbyte, buf);
		//sleep(1);

	}
	shutdown(sock, SHUT_RDWR);
}


int main(void)
{
	create_client(1);
	return 0;
}

